Class 5: Exporting Slides and Handouts

ORG 0000 Joon H. Ro & Jae-Eun Namkoong

Class 5 [2018-09-11 Tue]

1 Exporting Content in Org Mode

Org mode provides powerful export functionalities, which enable users to convert Org markup content to a variety of other formats. The outputs have proper formatting while maintaining the original structure and markup. The Org-Coursepack is set up to use reveal.js for slides and LaTeX for handouts. Users can adapt the current setting to accomodate other output formats.

General information about exporting can be found in the Org manual. Hence, this lecture focuses on describing how exporting is set up in the Org-Coursepack, introducing pre-built export functionalities of the Org-Coursepack, and offering useful exporting tips for instructors.

1.1 Setting Export Scope to Subtree

The export command, org-export-dispatch (C-c C-e), takes the user to the Org Export Dispatcher interface. Here, the user can select whether to export the buffer (i.e., the whole file) or only a subtree. An instructor may use the former to create a course booklet and the latter to export slides for a full or part of a lecture. The default scope of Org mode is Buffer, but a user can put the following snippet in the Emacs init file to set the default scope to Subtree.

;; set the default export scope to subtree
(setf org-export-initial-scope 'subtree)

1.2 Export Settings

Org mode allows users to specify export settings at both buffer and subtree levels.

1.2.1 Buffer-Level Settings

For buffer-level settings, one can use the #+OPTIONS: statement. For example, including the following line in an Org file will include a table of contents for that file in the exported document:

#+OPTIONS: toc:t

Similarly including the following line will add numbers in front of the headings.

#+OPTIONS: num:t

1.2.2 Subtree-Level Settings

Export settings can be specified at the subtree level with :PROPERTIES:. Simply add :EXPORT_: as a prefix to each option. For example, the title of the document can be set with :EXPORT_TITLE:. To specify multiple settings (e.g., items for LaTeX preamble), one can add + to the property name to append an additional value. For example,

:PROPERTIES:
:EXPORT_LATEX_HEADER+: \usepackage{titling}
:EXPORT_LATEX_HEADER+: \usepackage{multicol}
:END:

3 Exporting Slides: with reveal.js

4 Exporting Handouts: with LaTeX

LaTeX export is extensively supported by Org mode. We refer users to the Org manual for the in-depth instructions.

The following snippet shows the basic setup for our LaTeX output, where the koma-article class is added to org-latex-classes and the minted package is used for syntax highlighting. Currently, Python is the only language added to org-latex-minted-langs. Users can add to org-latex-minted-langs any other languages they want processed with the minted package.

Note

Note that we manually added the minted package to LaTeX preambles as opposed to adding it to org-latex-packages-alist. This was to allow for flexible specifications of the outputdir option.

(eval-after-load 'ox '(require 'ox-koma-letter))

(eval-after-load 'ox '(add-to-list 'org-latex-classes
                               '("koma-article"
                                 "\\documentclass{scrartcl}"
                                 ("\\section{%s}" . "\\section*{%s}")
                                 ("\\subsection{%s}" . "\\subsection*{%s}")
                                 ("\\subsubsection{%s}"
                                  . "\\subsubsection*{%s}")
                                 ("\\paragraph{%s}" . "\\paragraph*{%s}")
                                 ("\\subparagraph{%s}"
                                  . "\\subparagraph*{%s}"))) )

(require 'ox-latex)
(setq org-latex-listings 'minted)

(setq org-latex-pdf-process
      '("pdflatex -shell-escape -interaction nonstopmode -output-directory %o %f"
    "pdflatex -shell-escape -interaction nonstopmode -output-directory %o %f"))

(add-to-list 'org-latex-minted-langs '(python "python"))

4.1 Inserting a Page Break Before a Heading in LaTeX Export

Users can add a page break in the LaTeX export by inserting #+LATEX: \clearpage. Importantly, adding the following code into the init file automatically inserts a page break before any subtree that has a :newpage: tag.

(defun org/get-headline-string-element  (headline backend info)
  "Return the org element representation of an element.

  Won't work on ~verb~/=code=-only headers"
  (let ((prop-point (next-property-change 0 headline)))
    (if prop-point (plist-get (text-properties-at prop-point headline) :parent))))

(defun org/ensure-latex-clearpage (headline backend info)
  (when (org-export-derived-backend-p backend 'latex)
    (let ((elmnt (org/get-headline-string-element headline backend info)))
      (when (member "newpage" (org-element-property :tags elmnt))
    (concat "\\clearpage\n" headline)))))

(eval-after-load 'ox '(add-to-list
                   'org-export-filter-headline-functions
                   'org/ensure-latex-clearpage))

5 Selective Export

By using raw code and custom Emacs-lisp scripts, users can flexibly choose which content to show/hide, depending on output format. For example, instructors may want to show images in slides but not in handouts, or they may want to include supplementary notes in handouts but not in slides.

5.1 Tagging a Subtree as Slide or Handout Only

With the code below in added to your init file, you can use the :slideonly: or :handoutonly: tags to selectively include a subtree in either a slide output or handout output, respectively. Currently LaTeX and rst backends is set as a handout output, and reveal.js and beamer backends are set as slide outputs.

For example,

* This subtree will only be exported in slide output      :slideonly:
- Content
* This subtree will only be exported in handout output    :handoutonly:
- Content
(defun org/parse-headings (backend)
  "Remove every headline with certain tags in the
  current buffer. BACKEND is the export back-end being used, as
  a symbol.

  "

  (if (member backend '(latex rst))
      (org-map-entries
       (lambda ()
     (progn
       (org-narrow-to-subtree)
       (org-cut-subtree)
       (widen)
       ))
       "+slideonly"))

  (if (member backend '(reveal beamer))
      (org-map-entries
       (lambda ()
     (progn
       (org-narrow-to-subtree)
       (org-cut-subtree)
       (widen)
       ))
       "+handoutonly"))

)

(add-hook 'org-export-before-parsing-hook 'org/parse-headings)

5.2 Hiding Specific Content

To hide content when exporting to HTML-based format outputs (slides), use raw HTML tags <span hidden> and </span>. See the example below.

#+REVEAL_HTML: <span hidden>
This will not be shown in reveal.js output
#+REVEAL_HTML: </span>

Similarly, any content placed between \iffalse and \fi will not be rendered in LaTeX outputs (handouts). See the example below.

#+LATEX: \iffalse
This will not be shown in LaTeX output
#+LATEX: \fi